using System;
using System.Collections.Generic;
using System.Text;
using System.IO; // for streaming
using System.Drawing; // for graphics
using System.Drawing.Imaging; // for jpeg
using ZLib; // for compression

namespace cb0tServer
{
    class Avatar
    {
        public static byte[] sb0t_avatar_data;
        public static Bitmap sb0t_avatar_image;
        public static Bitmap blank;

        public static Bitmap default_av;
        public static byte[] default_av_bytes;

        public static void LoadDefault()
        {
            default_av = null;
            default_av_bytes = null;

            try
            {
                default_av_bytes = File.ReadAllBytes(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/davatar.dat");
                default_av = new Bitmap(new MemoryStream(default_av_bytes));
            }
            catch
            {
                default_av = null;
                default_av_bytes = null;
            }
        }

        public static void UpdateDefault(String path)
        {
            try
            {
                byte[] raw = File.ReadAllBytes(path);

                // resize

                Bitmap org_pic = new Bitmap(new MemoryStream(raw));
                Bitmap sized_pic = new Bitmap(48, 48);
                Graphics g = Graphics.FromImage(sized_pic);

                g.DrawImage(org_pic, new RectangleF(0, 0, 48, 48));

                // save

                MemoryStream stream = new MemoryStream();
                sized_pic.Save(stream, ImageFormat.Jpeg);
                default_av_bytes = stream.ToArray();
                default_av = new Bitmap(new MemoryStream(default_av_bytes));
                File.WriteAllBytes(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/davatar.dat", default_av_bytes);
            }
            catch
            {
                default_av = null;
                default_av_bytes = null;
            }
        }

        public static void Load()
        {
            sb0t_avatar_data = null;
            sb0t_avatar_image = null;

            blank = new Bitmap(48, 48);
            Graphics g = Graphics.FromImage(blank);
            g.FillRectangle(Brushes.Silver, new Rectangle(0, 0, 48, 48)); // set up default

            try
            {
                sb0t_avatar_data = File.ReadAllBytes(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/avatar.dat");
                sb0t_avatar_data = Zlib.Decompress(sb0t_avatar_data, false);
                sb0t_avatar_image = new Bitmap(new MemoryStream(sb0t_avatar_data));
            }
            catch
            {
                sb0t_avatar_data = null;
                sb0t_avatar_image = null;
            }
        }

        public static void Update(String path)
        {
            try
            {
                byte[] raw = File.ReadAllBytes(path);

                // resize

                Bitmap org_pic = new Bitmap(new MemoryStream(raw));
                Bitmap sized_pic = new Bitmap(48, 48);
                Graphics g = Graphics.FromImage(sized_pic);

                g.DrawImage(org_pic, new RectangleF(0, 0, 48, 48));

                // save

                MemoryStream stream = new MemoryStream();
                sized_pic.Save(stream, ImageFormat.Jpeg);
                sb0t_avatar_data = stream.ToArray();
                sb0t_avatar_image = new Bitmap(new MemoryStream(sb0t_avatar_data));
                byte[] sb0t_avatar_data_compressed = Zlib.Compress(sb0t_avatar_data);
                File.WriteAllBytes(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/avatar.dat", sb0t_avatar_data_compressed);
            }
            catch
            {
                sb0t_avatar_data = null;
                sb0t_avatar_image = null;
            }
        }
    }
}
